//
//  BCCoinListCell.swift
//  Blockfolio
//
//  Created by Maybe on 09/05/2021.
//

import UIKit
import SnapKit
import AlamofireImage

class BCCoinListCell: UITableViewCell {

//    override func awakeFromNib() {
//        super.awakeFromNib()
//        // Initialization code
//    }
    var coinModel: BCCoinModel? {
        didSet {
            guard let coinModel = coinModel else {
                return
            }
            topTitleLabel.text = coinModel.name
            topSubTitleLabel.text = coinModel.symbol?.uppercased()
                        
            priceLabel.text = "$ \(coinModel.current_price ?? " " )"
            
            let currentPrice = Double(coinModel.current_price ?? "")
            let changePrice = Double(coinModel.price_change_24h ?? "")
           
            let roate = changePrice!/currentPrice! * 100
            
            if roate > 0 {
                roateLabel.text = "+\(String(format: "%.2f", roate))%"
                roateLabel.textColor = UIColor.green
            }else {
                roateLabel.text = "\(String(format: "%.2f", roate))%"
                roateLabel.textColor = UIColor.red

            }
            
            
            let url = URL(string: coinModel.image ?? "")!
//            let placeholderImage = UIImage(named: "placeholder")!
            coinIcon.af.setImage(
                withURL: url
//                placeholderImage: placeholderImage,
            )
            
        }
    }
    

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    //coin 图标
    fileprivate lazy var coinIcon: UIImageView = {
       let imageView = UIImageView()
        imageView.backgroundColor = UIColor.clear
       return imageView
    }()
    
    fileprivate lazy var topTitleLabel: UILabel = {
        let label = UILabel()
        label.text = ""
        label.textColor = UIColor.white
        label.font = UIFont.boldSystemFont(ofSize: 16)
        return label
    }()
    
    fileprivate lazy var topSubTitleLabel: UILabel = {
        let label = UILabel()
        label.text = ""
        label.textColor = UIColor.lightGray
        label.font = UIFont.boldSystemFont(ofSize: 14)
        return label
    }()
    
    fileprivate lazy var priceLabel : UILabel = {
        let label = UILabel()
        label.text = ""
        label.textColor = UIColor.white
        label.font = UIFont.boldSystemFont(ofSize: 16)
        label.textAlignment = .right
        return label
    }()
    
    fileprivate lazy var roateLabel : UILabel = {
       let label = UILabel()
        label.text = ""
        label.textColor = UIColor.red
        label.font = UIFont.systemFont(ofSize: 14)
        label.textAlignment = .right
        return label
    }()
    
    
    
}


extension BCCoinListCell {
    
    fileprivate func setupUI(){
        selectionStyle = .none
        backgroundColor = UIColor.clear
        
        let containerView = UIView()
        containerView.backgroundColor = UIColor.hex("#1C1927")
        containerView.layer.cornerRadius = 8
        containerView.layer.masksToBounds = true
        contentView.addSubview(containerView)
        containerView.snp.makeConstraints { (make) in
            make.left.equalTo(24)
            make.top.equalTo(12)
            make.bottom.equalTo(0)
            make.right.equalTo(-24)
        }
        containerView.addSubview(coinIcon)
        containerView.addSubview(topTitleLabel)
        containerView.addSubview(topSubTitleLabel)
        containerView.addSubview(priceLabel)
        containerView.addSubview(roateLabel)
        
        coinIcon.snp.makeConstraints { (make) in
            make.centerY.equalTo(containerView.snp.centerY)
            make.left.equalTo(12)
            make.width.equalTo(20)
            make.height.equalTo(20)
        }
        topTitleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(12)
            make.left.equalTo(coinIcon.snp.right).offset(12)
            make.width.equalTo(180)
        }
        topSubTitleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(topTitleLabel.snp.bottom).offset(5)
            make.left.equalTo(topTitleLabel.snp.left)
            make.width.equalTo(topTitleLabel.snp.width)
        }
        priceLabel.snp.makeConstraints { (make) in
            make.top.equalTo(topTitleLabel.snp.top)
            make.right.equalTo(containerView.snp.right).offset(-12)
            make.width.equalTo(180)
        }
        
        roateLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(topSubTitleLabel.snp.centerY)
            make.right.equalTo(priceLabel.snp.right)
            make.width.equalTo(priceLabel.snp.width)
        }
    }
    override func layoutSubviews() {
        superview?.layoutSubviews()
        
    }
    
}
